package com.sls.web.manager.recharge.service.impl;

import java.lang.reflect.Type;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.recharge.pojo.SystemProduct;
import com.sls.web.manager.recharge.querybean.SystemProductQuery;
import com.sls.web.manager.recharge.service.SystemProductService;
import com.sls.web.manager.socket.SuperService;
import com.whalin.MemCached.MemCachedClient;

/**
 * @author sxf
 * 
 */
@Service("systemProductService")
public class SystemProductServiceImpl extends SuperService implements
		SystemProductService {
	private static Logger log = Logger
			.getLogger(SystemProductServiceImpl.class);
	@Resource(name = "memcachedClient")
	MemCachedClient memcachedClient;

	/**
	 * @param serviceCode
	 * @param sendObj
	 * @param userInfoVO
	 * @param classType
	 *            类型信息(如:String,为String.class)
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public <T> T callService(String serviceCode, Head head, Object sendObj,
			UserInfoVO userInfoVO, Object... classType) throws Exception {
		if (userInfoVO == null) {
			throw new ManagerException("用户信息为空");
		}
		Gson gson = CommonUtil.gson();
		if (head == null) {
			head = new Head();
			head.setRequestId(UUID.randomUUID().toString());
			head.setServiceCode(serviceCode);
			head.setIp(userInfoVO.getCurrentIp());
			head.setUserId(userInfoVO.getUserId());
		}
		SocketDTO requestDTO = super.send(head, sendObj);
		if (!TransmissionCode.SUCCESS.getCode().equals(
				requestDTO.getHead().getResponseCode())) {
			throw new ManagerException(requestDTO.getHead()
					.getResponseMessage());
		}
		String body = requestDTO.getBody();
		T t = null;
		if (body != null) {
			if (classType != null && classType.length > 0) {
				if (classType[0] instanceof Class) {
					t = gson.fromJson(body, (Class<T>) classType[0]);
				} else if (classType[0] instanceof Type) {
					t = gson.fromJson(body, (Type) classType[0]);
				}
			}
		}
		return t;
	}

	public boolean insert(SystemProduct info, UserInfoVO userInfoVO)
			throws Exception {
		this.callService(Constants.INSERT_SYSTEMPRODUCT, null, info, userInfoVO);
		return true;
	}

	public boolean update(SystemProduct info, UserInfoVO userInfoVO)
			throws Exception {
		this.callService(Constants.UPDATE_SYSTEMPRODUCT, null, info, userInfoVO);
		return true;
	}

	public boolean delete(SystemProduct info, UserInfoVO userInfoVO)
			throws Exception {
		this.callService(Constants.DELETE_SYSTEMPRODUCT, null, info, userInfoVO);
		return true;
	}

	@Override
	public List<SystemProduct> queryList(SystemProductQuery query,
			UserInfoVO userInfoVO) throws Exception {
		List<SystemProduct> infoPage = callService(
				Constants.QUERY_SYSTEMPRODUCT_LIST, null, query, userInfoVO,
				new TypeToken<List<SystemProduct>>() {
				}.getType());
		return infoPage;
	}

	public Pagination<SystemProduct> queryPage(SystemProductQuery query,
			UserInfoVO userInfoVO) throws Exception {
		Pagination<SystemProduct> infoPage = callService(
				Constants.QUERY_SYSTEMPRODUCT_PAGE, null, query, userInfoVO,
				new TypeToken<Pagination<SystemProduct>>() {
				}.getType());
		return infoPage;
	}

	public SystemProduct queryOne(SystemProductQuery query,
			UserInfoVO userInfoVO) throws Exception {
		log.info("Enter SystemProductService queryPage method...");
		SystemProduct infoPage = callService(Constants.QUERY_SYSTEMPRODUCT_ONE,
				null, query, userInfoVO, SystemProduct.class);
		log.info("Leave SystemProductService queryPage method...");
		return infoPage;
	}

	@Override
	public List<SystemProduct> queryCardMangementList(SystemProductQuery query,
			UserInfoVO userInfoVO) throws Exception {
		List<SystemProduct> infoPage = callService(
				Constants.QUERY_SYSTEMPRODUCT_CARDMANAGEMENT_LIST, null, query,
				userInfoVO, new TypeToken<List<SystemProduct>>() {
				}.getType());
		return infoPage;
	}

	public Pagination<SystemProduct> queryCardMangementPage(
			SystemProductQuery query, UserInfoVO userInfoVO) throws Exception {
		log.info("Enter SystemProductService queryPage method...");
		Pagination<SystemProduct> infoPage = callService(
				Constants.QUERY_SYSTEMPRODUCT_CARDMANAGEMENT_PAGE, null, query,
				userInfoVO, new TypeToken<Pagination<SystemProduct>>() {
				}.getType());
		log.info("Leave SystemProductService queryPage method...");
		return infoPage;
	}

	public SystemProduct queryCardMangementOne(SystemProductQuery query,
			UserInfoVO userInfoVO) throws Exception {
		log.info("Enter SystemProductService queryPage method...");
		SystemProduct infoPage = callService(
				Constants.QUERY_SYSTEMPRODUCT_CARDMANAGEMENT_ONE, null, query,
				userInfoVO, SystemProduct.class);
		log.info("Leave SystemProductService queryPage method...");
		return infoPage;
	}

	@Override
	public List<SystemProduct> queryListProductTypeCardStock(
			UserInfoVO userInfoVO) throws Exception {
		SystemProductQuery query = new SystemProductQuery();
		query.setProductType(0);// 卡密
		return queryList(query, userInfoVO);
	}

	@Override
	public List<SystemProduct> queryListProductTypeFling(UserInfoVO userInfoVO)
			throws Exception {
		SystemProductQuery query = new SystemProductQuery();
		query.setProductType(1);// 直冲产品
		return queryList(query, userInfoVO);
	}

	public SystemProduct queryOneById(String id, UserInfoVO userInfoVO)
			throws Exception {
		SystemProductQuery query = new SystemProductQuery();
		query.setId(id);
		return queryOne(query, userInfoVO);
	}

	@Override
	public List<SystemProduct> findRechargeList(UserInfoVO userInfoVO)
			throws Exception {
		List<SystemProduct> list = callService(Constants.RECHARGE_LIST, null,
				null, userInfoVO, new TypeToken<List<SystemProduct>>() {
				}.getType());
		memcachedClient.set(Constants.SYSTEM_PRODUCT, list);
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public SystemProduct findRechargeById(String id, UserInfoVO userInfoVO)
			throws Exception {
		Object object = memcachedClient.get(Constants.SYSTEM_PRODUCT);
		List<SystemProduct> list = null;
		if (object instanceof List) {
			list = (List<SystemProduct>) object;
		}
		if (list != null && list.size() > 0) {
			for (SystemProduct systemProduct : list) {
				if (systemProduct.getId().equals(id)) {
					return systemProduct;
				}
			}
		} else {
			SystemProduct sp = new SystemProduct();
			sp.setId(id);
			return callService(Constants.RECHARGE_ONE, null, sp, userInfoVO,
					SystemProduct.class);
		}

		return null;

	}

	@Override
	public void insertRecharge(SystemProduct sp, UserInfoVO userInfoVO)
			throws Exception {
		callService(Constants.RECHARGE_INSERT, null, sp, userInfoVO,
				SystemProduct.class);
	}

	@Override
	public void updateRecharge(SystemProduct sp, UserInfoVO userInfoVO)
			throws Exception {
		callService(Constants.RECHARGE_UPDATE, null, sp, userInfoVO,
				SystemProduct.class);
	}

	@Override
	public void deletedRecharge(String id, UserInfoVO userInfoVO)
			throws Exception {
		SystemProduct sp = new SystemProduct();
		sp.setId(id);
		callService(Constants.RECHARGE_DELETED, null, sp, userInfoVO,
				SystemProduct.class);
	}
}
